@echo off
ECHO Computing CRC
ECHO -------------------------------------

SET SREC_PATH=..\SREC
SET TARGET_NAME=CZSR8903
SET TARGET_PATH=obj

SET COMPARE_HEX=1
SET CRC_ADDR_FROM_MAP=1

REM Derived configuration
SET MAP_FILE=lst\%TARGET_NAME%.map
SET INPUT_HEX=%TARGET_PATH%\%TARGET_NAME%.hex
SET OUTPUT_HEX=%TARGET_PATH%\%TARGET_NAME%_SIL.hex
SET TMP_FILE=crc_tmp_file.txt

IF NOT "%CRC_ADDR_FROM_MAP%"=="1" goto:end_of_map_extraction


ECHO Extracting CRC address from MAP file

FINDSTR /R /C:"^  *CHECKSUM" %MAP_FILE%>%TMP_FILE%
SET /p crc_search=<%TMP_FILE%
DEL %TMP_FILE%

REM remove CHECKSUM string from variable
for /f "tokens=1 delims=(" %%a in ("%crc_search%") do set crc_search=%%a
REM remove CHECKSUM string from variable
SET crc_search=%crc_search:CHECKSUM=%
REM get first word at line, which should be CRC address in HEX format
for /f "tokens=1 delims= " %%a in ("%crc_search%") do set CRC_ADDR=%%a
REM -----------------------------------------------------------
REM End of CRC address extraction

:end_of_map_extraction

REM Compute CRC and store it to new HEX file
ECHO CRC address: %CRC_ADDR%

SET STEPPING=(%CRC_ADDR% - 0x08000000)/8

SET/a ADDR_START=0x08000000

SET/a ADDR_END=0x08000000+%STEPPING%


REM crc0
ECHO %SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel
%SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel

ECHO %SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %TARGET_PATH%\%TARGET_NAME%0.SIL.hex -intel
%SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %TARGET_PATH%\%TARGET_NAME%0.SIL.hex -intel

SET/a ADDR_START=%ADDR_START%+%STEPPING%
SET/a ADDR_END=%ADDR_END%+%STEPPING%
SET/a CRC_ADDR=%CRC_ADDR%+4

REM crc1
ECHO %SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel
%SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel

ECHO %SREC_PATH%\srec_cat.exe ^
	%TARGET_PATH%\%TARGET_NAME%0.SIL.hex -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %TARGET_PATH%\%TARGET_NAME%1.SIL.hex -intel
%SREC_PATH%\srec_cat.exe ^
	%TARGET_PATH%\%TARGET_NAME%0.SIL.hex -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %TARGET_PATH%\%TARGET_NAME%1.SIL.hex -intel

SET/a ADDR_START=%ADDR_START%+%STEPPING%
SET/a ADDR_END=%ADDR_END%+%STEPPING%
SET/a CRC_ADDR=%CRC_ADDR%+4

REM crc2
ECHO %SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel
%SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel

ECHO %SREC_PATH%\srec_cat.exe ^
	%TARGET_PATH%\%TARGET_NAME%1.SIL.hex -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %TARGET_PATH%\%TARGET_NAME%2.SIL.hex -intel
%SREC_PATH%\srec_cat.exe ^
	%TARGET_PATH%\%TARGET_NAME%1.SIL.hex -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %TARGET_PATH%\%TARGET_NAME%2.SIL.hex -intel

SET/a ADDR_START=%ADDR_START%+%STEPPING%
SET/a ADDR_END=%ADDR_END%+%STEPPING%
SET/a CRC_ADDR=%CRC_ADDR%+4


REM crc3
ECHO %SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel
%SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel

ECHO %SREC_PATH%\srec_cat.exe ^
	%TARGET_PATH%\%TARGET_NAME%2.SIL.hex -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %TARGET_PATH%\%TARGET_NAME%3.SIL.hex -intel
%SREC_PATH%\srec_cat.exe ^
	%TARGET_PATH%\%TARGET_NAME%2.SIL.hex -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %TARGET_PATH%\%TARGET_NAME%3.SIL.hex -intel

SET/a ADDR_START=%ADDR_START%+%STEPPING%
SET/a ADDR_END=%ADDR_END%+%STEPPING%
SET/a CRC_ADDR=%CRC_ADDR%+4


REM crc4
ECHO %SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel
%SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel

ECHO %SREC_PATH%\srec_cat.exe ^
	%TARGET_PATH%\%TARGET_NAME%3.SIL.hex -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %TARGET_PATH%\%TARGET_NAME%4.SIL.hex -intel
%SREC_PATH%\srec_cat.exe ^
	%TARGET_PATH%\%TARGET_NAME%3.SIL.hex -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %TARGET_PATH%\%TARGET_NAME%4.SIL.hex -intel

SET/a ADDR_START=%ADDR_START%+%STEPPING%
SET/a ADDR_END=%ADDR_END%+%STEPPING%
SET/a CRC_ADDR=%CRC_ADDR%+4


REM crc5
ECHO %SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel
%SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel

ECHO %SREC_PATH%\srec_cat.exe ^
	%TARGET_PATH%\%TARGET_NAME%4.SIL.hex -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %TARGET_PATH%\%TARGET_NAME%5.SIL.hex -intel
%SREC_PATH%\srec_cat.exe ^
	%TARGET_PATH%\%TARGET_NAME%4.SIL.hex -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %TARGET_PATH%\%TARGET_NAME%5.SIL.hex -intel

SET/a ADDR_START=%ADDR_START%+%STEPPING%
SET/a ADDR_END=%ADDR_END%+%STEPPING%
SET/a CRC_ADDR=%CRC_ADDR%+4


REM crc6
ECHO %SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel
%SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel

ECHO %SREC_PATH%\srec_cat.exe ^
	%TARGET_PATH%\%TARGET_NAME%5.SIL.hex -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %TARGET_PATH%\%TARGET_NAME%6.SIL.hex -intel
%SREC_PATH%\srec_cat.exe ^
	%TARGET_PATH%\%TARGET_NAME%5.SIL.hex -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %TARGET_PATH%\%TARGET_NAME%6.SIL.hex -intel

SET/a ADDR_START=%ADDR_START%+%STEPPING%
SET/a ADDR_END=%ADDR_END%+%STEPPING%
SET/a CRC_ADDR=%CRC_ADDR%+4


REM crc7
ECHO %SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel
%SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %ADDR_START% %ADDR_END% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel

ECHO %SREC_PATH%\srec_cat.exe ^
	%TARGET_PATH%\%TARGET_NAME%6.SIL.hex -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %OUTPUT_HEX% -intel
%SREC_PATH%\srec_cat.exe ^
	%TARGET_PATH%\%TARGET_NAME%6.SIL.hex -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %OUTPUT_HEX% -intel



DEL %TMP_FILE%
DEL %TARGET_PATH%\%TARGET_NAME%0.SIL.hex
DEL %TARGET_PATH%\%TARGET_NAME%1.SIL.hex
DEL %TARGET_PATH%\%TARGET_NAME%2.SIL.hex
DEL %TARGET_PATH%\%TARGET_NAME%3.SIL.hex
DEL %TARGET_PATH%\%TARGET_NAME%4.SIL.hex
DEL %TARGET_PATH%\%TARGET_NAME%5.SIL.hex
DEL %TARGET_PATH%\%TARGET_NAME%6.SIL.hex



ECHO Modified HEX file with CRC stored at %OUTPUT_HEX%



REM Compare input HEX file with output HEX file
ECHO Comparing %INPUT_HEX% with %OUTPUT_HEX%
%SREC_PATH%\srec_cmp.exe ^
	%INPUT_HEX% -intel %OUTPUT_HEX% -intel -v



ECHO -------------------------------------